#include "TrivialApplication.h"

static jclass biclass = NULL;
static jmethodID bimid;

JNIEXPORT void JNICALL Java_TrivialApplication_dumbnativecopy
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){
	long *int1;
	long *int2;
	int1 = (long *)malloc(sizeof(long)*size);
	int2 = (long *)malloc(sizeof(long)*size);
	env->GetIntArrayRegion(i1,0,size,int1);
	for(int i=0;i<size;i++){
		int2[i] = int1[i];
	}
	env->SetIntArrayRegion(i2,0,size,int2);		
	free(int1);
	free(int2);
}	

JNIEXPORT void JNICALL Java_TrivialApplication_lessdumbnativecopy
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){
	long *int1;
	long *int2;
	int1 = (long *)malloc(sizeof(long)*size);
	int2 = (long *)malloc(sizeof(long)*size);
	env->GetIntArrayRegion(i1,0,size,int1);
	for(int i=0;i<size;i++){
		*int2++ = *int1++;
	}
	env->SetIntArrayRegion(i2,0,size,int2);		
	free(int1);
	free(int2);
}	

JNIEXPORT void JNICALL Java_TrivialApplication_nativedonothing
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){
	long *int1;
	long *int2;
	int1 = (long *)malloc(sizeof(long)*size);
	int2 = (long *)malloc(sizeof(long)*size);
	env->GetIntArrayRegion(i1,0,size,int1);
	env->SetIntArrayRegion(i2,0,size,int2);		
	free(int1);
	free(int2);
}	

JNIEXPORT void JNICALL Java_TrivialApplication_nativedoabsolutelynothing
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){


}

JNIEXPORT void JNICALL Java_TrivialApplication_nativecritical
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){

	int *int1 = (int *)env->GetPrimitiveArrayCritical(i1,0);
	int *int2 = (int *)env->GetPrimitiveArrayCritical(i2,0);
	int count = size*sizeof(jint)/sizeof(int);
	int *iptr1 = int1;
	int *iptr2 = int2;
	for(int i=0;i<count;i++){
		*iptr2++ = *iptr1++;
	}	
	env->ReleasePrimitiveArrayCritical(i1,int1,0);
	env->ReleasePrimitiveArrayCritical(i2,int2,0);
	
}	

JNIEXPORT void JNICALL Java_TrivialApplication_nativecriticalmemcpy
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){

	int *int1 = (int *)env->GetPrimitiveArrayCritical(i1,0);
	int *int2 = (int *)env->GetPrimitiveArrayCritical(i2,0);
	int count = size*sizeof(jint)/sizeof(int);
	int *iptr1 = int1;
	int *iptr2 = int2;
	memcpy(iptr2,iptr1,count*sizeof(int));	
	env->ReleasePrimitiveArrayCritical(i1,int1,0);
	env->ReleasePrimitiveArrayCritical(i2,int2,0);
	
}	

JNIEXPORT void JNICALL Java_TrivialApplication_nativepullonly
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){
	long *int1;
	int1 = (long *)malloc(sizeof(long)*size);
	env->GetIntArrayRegion(i1,0,size,int1);
	free(int1);
}	

JNIEXPORT void JNICALL Java_TrivialApplication_nativecriticalpullonly
	(JNIEnv *env, jclass myclass, jintArray i1, jintArray i2, jint size){

	int *int1 = (int *)env->GetPrimitiveArrayCritical(i1,0);
	env->ReleasePrimitiveArrayCritical(i1,int1,0);	
}	